<!DOCTYPE html>
<!--
  Copyright 2016 Google Inc. All rights reserved.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      https://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License
-->
<html>
  <head>
    <meta charset="utf-8">
    <title>MDL Simple Menu Demo</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1">
    <link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500" rel="stylesheet">
    <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
    <script src="assets/material-design-lite.css.js" charset="utf-8"></script>
    <style>
      html,
      body,
      main {
        display: flex;
        flex-direction: column;
        height: 100%;
      }

      .demo-content {
        position: relative;
        flex: 1;
        margin-top: 16px;
        background-color: antiquewhite;
      }

      .mdl-menu-anchor {
        position: absolute;
      }
    </style>
  </head>
  <body>
    <main>
      <h1>MDL Simple Menu</h1>
      <p>
        Button position:
        <label>
          <input type="radio" name="position" value="top left" checked>
          Top left
        </label>
        <label>
          <input type="radio" name="position" value="top right">
          Top right
        </label>
        <label>
          <input type="radio" name="position" value="bottom left">
          Bottom left
        </label>
        <label>
          <input type="radio" name="position" value="bottom right">
          Bottom right
        </label>
      </p>
      <p>
        <label>
          <input type="checkbox" name="dark">
          Dark mode
        </label>
      </p>
      <div>
        <span>Last Selected item: <em id="last-selected">&lt;none selected&gt;</em></span>
      </div>
      <div class="demo-content">
        <div class="mdl-menu-anchor">
          <button class="toggle">Toggle</button>

          <div class="mdl-simple-menu" style="position: absolute;" tabindex="-1">
            <ul class="mdl-simple-menu__items mdl-list" role="menu" aria-hidden="true">
              <li class="mdl-list-item" role="menuitem" tabindex="0">Back</li>
              <li class="mdl-list-item" role="menuitem" tabindex="0">Forward</li>
              <li class="mdl-list-item" role="menuitem" tabindex="0">Reload</li>
              <li class="mdl-list-divider" role="separator"></li>

              <li class="mdl-list-item" role="menuitem" tabindex="0">Save As...</li>
              <li class="mdl-list-item" role="menuitem" tabindex="0">Print...</li>
              <li class="mdl-list-item" role="menuitem" tabindex="0">Cast...</li>
              <li class="mdl-list-item" role="menuitem" tabindex="0">Translate to English</li>
              <li class="mdl-list-divider" role="separator"></li>

              <li class="mdl-list-item" role="menuitem" tabindex="0">View Page Source</li>
              <li class="mdl-list-item" role="menuitem" tabindex="0">Inspect</li>
            </ul>
          </div>
        </div>
      </div>
    </main>

    <script src="../assets/material-design-lite.js" charset="utf-8"></script>
    <script>
      var menuEl = document.querySelector('.mdl-simple-menu');
      var menu = new mdl.menu.MDLSimpleMenu(menuEl);
      var toggle = document.querySelector('.toggle');
      toggle.addEventListener('click', function() {
        menu.open = !menu.open;
      });

      var dark = document.querySelector('input[name="dark"]');
      dark.addEventListener('change', function(evt) {
        if (evt.target.checked) {
          menuEl.classList.add('mdl-simple-menu--theme-dark');
        } else {
          menuEl.classList.remove('mdl-simple-menu--theme-dark');
        }
      });

      var radios = document.querySelectorAll('input[name="position"]');
      for (var i = 0; i < radios.length; i++) {
        radios[i].addEventListener('change', function(evt) {
          if (evt.target.checked) {
            if (evt.target.value) {
              var anchor = document.querySelector('.mdl-menu-anchor');
              anchor.style.removeAttribute('top');
              anchor.style.removeAttribute('right');
              anchor.style.removeAttribute('bottom');
              anchor.style.removeAttribute('left');

              var vertical = evt.target.value.split(' ')[0];
              var horizontal = evt.target.value.split(' ')[1];
              anchor.style.setAttribute(vertical, '0');
              anchor.style.setAttribute(horizontal, '0');
            }
          }
        });
      }

      var lastSelected = document.getElementById('last-selected');
      menuEl.addEventListener('MDLSimpleMenu:selected', function(evt) {
        const detail = evt.detail;
        lastSelected.textContent = '"' + detail.item.textContent.trim() +
          '" at index ' + detail.index;
      });
    </script>
  </body>
</html>
